<?php
namespace app\index\controller;
use app\index\model\Archives;
use app\index\model\ArchivesCategory;
use app\index\model\Collect_model;
use app\index\model\Storeroom;
use app\index\model\Tag;
use app\index\model\TagArchivesRelation;
use app\index\model\TagGroup;
use think\Db;
use think\Request;

class Col extends Common {
	protected $request;
	protected $ac_model;
	protected $a_model;
	protected $tag_model;
	protected $tag_group_model;
	protected $tag_archives_relation_model;
	protected $storage_model;
	public function __construct(Request $request = null) {
		parent::__construct($request);
		$this->request = $request;
		$this->ac_model = new ArchivesCategory();
		$this->a_model = new Archives();
		$this->tag_model = new Tag();
		$this->tag_archives_relation_model = new TagArchivesRelation();
		$this->tag_group_model = new TagGroup();
		$this->storage_model = new Storeroom();
	}

	// 我的收藏
	public function index() {
		$this->getAuth();

		$rev = new Collect_model;
		$res = $rev->getindex();
		if ($res == '0') {
			return view('index@Col/index');
		} else {
			return view('index@Col/index', [
				'record' => $res['list'],
				'page' => $res['page'],
			]);
		}
	}

	// 取消收藏
	public function cancel() {
		// var_dump($_POST);die;
		$rev = new Collect_model;
		$res = $rev->cancelcol();
		return $res;
		// var_dump($res);die;
	}

	//在线阅读
	public function onRead() {
		$params = array();
		$id = $this->request->param('id', 0);
		// $id = '65';
		// var_dump($id);die;
		if (empty($id)) {
			jsonOut(0, '参数错误');
		}
		$data = $this->a_model->getDataByCondition(array('arc_id' => $id));
		$mark = Db::table('cn_archives')
		// ->where('file_new_name', 'like', '%' . $file['token'] . '%')
			->where('arc_id', $id)
			->field('mark')
		// ->order('file_id desc')
			->find();
		if (in_array($mark['mark'], ['2', '3', '4'])) {
			$extend_data = Db::table('cn_arc_extend_info')
				->alias('ei')
				->join('cn_arc_extend e', 'e.id = ei.arc_id')
			// ->where('file_new_name', 'like', '%' . $file['token'] . '%')
				->where('extend_id', $id)
			// ->field('arc_id,value')
			// ->order('file_id desc')
				->select();
		}
		// $extend_data = $this->aei_model->where(array('arc_id' => $id))->find();
		// var_dump($data);die;
		if (empty($data)) {
			jsonOut(0, '文件不存在');
		}
		$res = $this->acf_model->where(array('arc_id' => $id))->find();
		$res1 = $this->fl_model->where(array('file_id' => $res['file_id']))->find();
		$filePath = $res1['file_save_path'];
		// $filePath = $this->getFilePathUrl($data['category_id'], 2);
		// $filePath = $filePath['path'] . '/' . $data['arc_name'] . '.' . $data['arc_type'];
		$data['filePath'] = $filePath;
		$data['http'] = 'http://' . $_SERVER['HTTP_HOST'];
		$data['box_path'] = $this->storage_model->getStoragePath($data['box_id']);
		if ($data['arc_time'] == '0000-00-00 00:00:00') {
			$data['arc_time'] = date('Y-m-d');
		} else {
			$data['arc_time'] = date('Y-m-d', strtotime($data['arc_time']));
		}
		$params['data'] = $data;
		if (isset($extend_data)) {
			$params['extend_data'] = $extend_data;
		}
		$ac_data = $this->storage_model->where(array('is_delete' => 0))->select();
		$cate_tree = recursionStorage($ac_data, 'sto_id', 0, 'patent_id');
		$tree = createStorageTree($cate_tree, 'sto_id', 'patent_id', 'sto_name');
		if (empty($tree)) {
			jsonOut(0, '文件目录错误');
		}
		$params['tree'] = $tree;
		return view('index@Col/onRead', $params);
	}

}
